<?php

require_once("View/View/PageView.php");
require_once("login/src/View/FirstPage.php");
require_once("login/src/Model/LoginValidation.php");

class MasterController
{
	/**
	*@var PageView 
	**/
	private $pageView;
	/**
	*@var FirstPage 
	**/
	private $firstPage;
	/**
	*@var LoginValidation 
	**/
	private $loginValidation;


	public function __construct()
	{
		$this->pageView = new PageView();
		$this->loginValidation = new LoginValidation();
		$this->firstPage = new FirstPage($this->loginValidation);
	}

	/**
	*@return string
	**/
	public function getPage()
	{	
		$this->loginValidation->DefaultIPandUserAgentValues();

		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{	
			$username = $this->firstPage->getUsername();
			$password = $this->firstPage->getPassword();

			$valid = $this->loginValidation->validateLoginCredentials($username, $password);
			
			if($valid)
			{
				$this->loginValidation->login();
			}
		}
		else
		{
			$weJustLoggedOut = $this->loginValidation->didWeJustLogOut();
			$badSession = $this->loginValidation->CheckForValidSessions();
			
			if(!$weJustLoggedOut && !$badSession && $this->loginValidation->CheckIfWereLoggedIn())
			{	
				$this->loginValidation->login();
			}
		}
		
		$html = $this->getFirstPage();

		return $html;
	}

	/**
	*@return string
	**/
	private function getFirstPage()
	{
		$title = "Träningsdagboken";
		$head = "";
		$body = $this->getFrontPageBody();

		$html = $this->pageView->getHtmlPage($title, $head, $body);	

		return $html;
	}

	/**
	*@return string
	**/
	private function getFrontPageBody()
	{
		return $this->firstPage->getFirstPage();

	}



}